<?php
	
  
  logger()->info("\n\r\n\rPrivileges:");
  
  
  # PHP conf file load
  PreCompiler::compile_and_require_file('config' . S . 'privileges.php', ROOT_DIR);
  
  # Privelege restrictions process
  function check_access($path) {
  
    $access = true;    if (!$GLOBALS['Privileges']) $GLOBALS['Privileges'] = array();
    foreach ($GLOBALS['Privileges'] as $url_schema => $priv_set) {
      $priv_set = array_reverse($priv_set);
      $preg_url_schema = $url_schema;
      $preg_url_schema = str_replace('*', '.*', $preg_url_schema);
      $preg_url_schema = str_replace('/', '\/', $preg_url_schema);
      $preg_url_schema = '/^' . $preg_url_schema . '$/';
      if (preg_match($preg_url_schema, $path)) {
        foreach ($priv_set as $priv) {
          if (current($priv) == true) {
            switch (key($priv)) {
              case allow:
                logger()->info("  {$url_schema} => allowed");
                $access = true;
              break;
              case deny:
                logger()->info("  {$url_schema} => denied");
                $access = false;
              break;
            }
            break;
          }
        }
      }
    }
    return $access;
  }
  
  # if no access die "DENIED" message
  $access = check_access(REQUEST_PATH);
  if ($access == false) {
    echo("<h1>ACCESS DENIED</h1>");
    logger()->alert("Access denied");
  } else {
    logger()->info("  Access granted");
  }
  
  
?>